<?php
/**
 * Created by JetBrains PhpStorm.
 * User: ncduc
 * Date: 7/6/13
 * Time: 11:10 PM
 * To change this template use File | Settings | File Templates.
 */
include_once("../libs/setup.php");
include_once("../libs/helper.php");
session_start();

if (!check_user_session()) {
    header("Location: ../signin.php");
    return;
}
assign_home_header($smarty);
$smarty->assign('wwwUrl','../');
//functions
function load_more_infos($userId, mysqli $mysqli, Smarty $smarty)
{
    $query = "select * from thong_tin_them where MaUser = $userId";
    $result = $mysqli->query($query);
    if($result)
    {
        while ($row = $result->fetch_assoc())
        {
            $smarty->assign('id',$row['Ma']);
            $smarty->assign('read_en',$row['TiengAnh_Doc']);
            $smarty->assign('write_en',$row['TiengAnh_Viet']);
            $smarty->assign('speak_en',$row['TiengAnh_Noi']);
            $smarty->assign('read_other',$row['Khac_Doc']);
            $smarty->assign('write_other',$row['Khac_Viet']);
            $smarty->assign('speak_other',$row['Khac_Noi']);
            $smarty->assign('degree_en_uploaded',$row['ChungChi_Anh']);
            $smarty->assign('degree_other_uploaded',$row['ChungChi_Khac']);
            $smarty->assign('health',$row['TinhTrangSucKhoe']);
            $smarty->assign('health_uploaded',$row['GiayKhamSucKhoe']);
            $smarty->assign('drl_uploaded',$row['GiayCNDRL']);
            $smarty->assign('moreinfo_vi',$row['ThongTinThem_vi']);
            $smarty->assign('moreinfo_en',$row['ThongTinThem_en']);
            $smarty->assign('profile_uploaded',$row['SoYeuLyLichUpload']);            
        }
    }
    if($mysqli->error)
    {
        system_failed($mysqli->error . "sql: " . $query, $smarty);
        $mysqli->close();
        return false;
    }
    return true;
}
function load_social_activities($userId, mysqli $mysqli, Smarty $smarty)
{
    $query = "select * from hoat_dong_xa_hoi where MaUser=$userId";
    $result = $mysqli->query($query);
    if($result)
    {
        $social_activities = array();
        while ($row = $result->fetch_assoc())
        {
            $social_activities[] = array(
              'Ma' => $row['Ma'],
                'TenHoatDong' => $row['TenHoatDong'],
                'TuNgay' => $row['TuNgay'],
                'DenNgay' => $row['DenNgay'],
                'DiaChi' => $row['DiaChi'],
                'VaiTro' => $row['VaiTro'],
                'GiayChungNhan' => $row['GiayChungNhan'],
            );
        }
    }
    $smarty->assign('social_activities',$social_activities);
    if($mysqli->error)
    {
        return false;
    }
    return true;
}
function save_more_info($userId, mysqli $mysqli, Smarty $smarty)
{
    
    $read_en = addslashes($_REQUEST['read_en']);
    $write_en = addslashes($_REQUEST['write_en']);
    $speak_en = addslashes($_REQUEST['speak_en']);
    $read_other =addslashes($_REQUEST['read_other']);
    $write_other = addslashes($_REQUEST['write_other']);
    $speak_other = addslashes($_REQUEST['speak_other']);
    $health = addslashes($_REQUEST['health']);
    $moreinfo_vi = addslashes($_REQUEST['moreinfo_vi']);
    $moreinfo_en = addslashes($_REQUEST['moreinfo_en']);
    $sql = "update thong_tin_them set TiengAnh_Doc={$read_en},TiengAnh_Noi={$speak_en},TiengAnh_Viet={$write_en},Khac_Doc={$read_other},Khac_Noi={$speak_other},Khac_Viet={$write_other},TinhTrangSucKhoe={$health},ThongTinThem_en='{$moreinfo_en}',ThongTinThem_vi='{$moreinfo_vi}' where MaUser=$userId";
    $result = $mysqli->query($sql);
    if($result)
    {
        $message = "Đã cập nhật thành công thông tin thêm";
        $smarty->assign('is_display', "block");
        $smarty->assign('type_message','success');
        $smarty->assign('message', $message);
    }
    if($mysqli->error)
    {
        system_failed($mysqli->error . "sql: " . $sql, $smarty);
        $mysqli->close();
        return false;
    }
    return true;
}

$mysqli = connect_database($smarty);
$cur_userId = $_SESSION['ma_user'];
if (empty($_REQUEST['save']) && empty($_REQUEST['next'])) {
    if(!load_more_infos($cur_userId,$mysqli,$smarty))
        return;

} else {
    if (!empty($_REQUEST['save'])) {
        if(!save_more_info($cur_userId,$mysqli,$smarty))
            return;
        if($_FILES['degree_en_upload']['size']>0)
        {
            if(!save_upload_file('degree_en_upload', 'thong_tin_them', 'ChungChi_Anh', $_REQUEST['id'], $mysqli, $smarty))
            {
                return;
            }
        }
        if($_FILES['degree_other_upload']['size']>0)
        {
            if(!save_upload_file('degree_other_upload', 'thong_tin_them', 'ChungChi_Khac', $_REQUEST['id'], $mysqli, $smarty))
            {
                return;
            }
        }
        if($_FILES['health_upload']['size']>0)
        {
            if(!save_upload_file('health_upload', 'thong_tin_them', 'GiayKhamSucKhoe', $_REQUEST['id'], $mysqli, $smarty))
            {
                return;
            }
        }
        if($_FILES['drl_upload']['size']>0)
        {
            if(!save_upload_file('drl_upload', 'thong_tin_them', 'GiayCNDRL', $_REQUEST['id'], $mysqli, $smarty))
            {
                return;
            }
        }
        if($_FILES['profile_upload']['size']>0)
        {
            if(!save_upload_file('profile_upload', 'thong_tin_them', 'SoYeuLyLichUpload', $_REQUEST['id'], $mysqli, $smarty))
            {
                return;
            }
        }
    } else {
        header("Location: 1.php");
    }
}
//load data table
if(!load_more_infos($cur_userId, $mysqli, $smarty))
        return;
if(!load_social_activities($cur_userId,$mysqli,$smarty))
    return;
$smarty->display('profile/5.tpl');